//
//  FullScreenMediaView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/3/2.
//

import SwiftUI

import SwiftUI
import AVKit
import Kingfisher
import SDWebImageSwiftUI

struct FullScreenMediaView: View {
    let mediaItems: [MediaItem]
    @Binding var currentIndex: Int
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        ZStack {
            Color.black.ignoresSafeArea()
            
            TabView(selection: $currentIndex) {
                ForEach(mediaItems.indices, id: \.self) { index in
                    MediaContentView(item: mediaItems[index])
                        .tag(index)
                }
            }
            .tabViewStyle(.page(indexDisplayMode: .never))
            
            VStack {
                HStack {
                    Spacer()
                    Button(action: {
                        presentationMode.wrappedValue.dismiss()
                    }) {
                        Image(systemName: "xmark")
                            .foregroundColor(.white)
                            .padding()
                            .background(Circle().fill(Color.black.opacity(0.5)))
                    }
                    .padding()
                }
                Spacer()
            }
        }
    }
}

struct MediaContentView: View {
    let item: MediaItem
    @State private var player: AVPlayer?
    @State private var scale: CGFloat = 1.0 // 用于缩放比例
    @State private var offset: CGSize = .zero // 用于拖动的偏移量
    @State private var accumulatedOffset: CGSize = .zero // 用于记录拖动前的偏移量

    var body: some View {
        Group {
            switch item.type {
            case .networkImage(_, let urlString, let isVideo):
                if isVideo {
                    if let url = URL(string: urlString.formatCdn()) {
                        MyVideoView(url: url)
                    }
                } else {
                    if let url = URL(string: urlString.formatCdn()) {
                        ZoomableKFImage(url: url)
                    }
                }
            case .localImage(let uiImage):
                ZoomableImage(image: Image(uiImage: uiImage))
            case .localImagePath(let fileUrlPath):
                ZoomableKFImage(url: URL(fileURLWithPath: fileUrlPath))
            case .localVideo(let url):
                MyVideoView(url: url)
            }
        }
    }
}


struct ZoomableImage: View {
    let image: Image // 支持本地图片
    @State private var scale: CGFloat = 1.0 // 缩放比例
    @State private var offset: CGSize = .zero // 图片的总偏移量
    @State private var accumulatedOffset: CGSize = .zero // 用于记录拖动前的偏移量

    var body: some View {
        image
            .resizable()
            .scaledToFit()
            .scaleEffect(scale) // 应用缩放
            .offset(offset) // 应用拖动偏移
            .gesture(
                SimultaneousGesture(
                    MagnificationGesture()
                        .onChanged { value in
                            scale = value
                        }
                        .onEnded { _ in
                            // 可选：限制缩放范围
                            scale = max(1.0, scale)
                        },
                    DragGesture()
                        .onChanged { value in
                            // 基于拖动开始时的偏移量计算当前偏移
                            offset = CGSize(
                                width: accumulatedOffset.width + value.translation.width,
                                height: accumulatedOffset.height + value.translation.height
                            )
                        }
                        .onEnded { value in
                            // 更新累计偏移量
                            accumulatedOffset = offset
                        }
                )
            )
            .animation(.easeInOut, value: scale) // 添加平滑动画
    }
}

struct ZoomableKFImage: View {
    let url: URL // 本地文件路径
    @State private var scale: CGFloat = 1.0 // 缩放比例
    @State private var offset: CGSize = .zero // 图片的总偏移量
    @State private var accumulatedOffset: CGSize = .zero // 用于记录拖动前的偏移量

    var body: some View {
        AnimatedImage(url: url)
            .resizable()
            .scaledToFit()
            .scaleEffect(scale) // 应用缩放
            .offset(offset) // 应用拖动偏移
            .gesture(
                SimultaneousGesture(
                    MagnificationGesture()
                        .onChanged { value in
                            scale = value
                        }
                        .onEnded { _ in
                            // 可选：限制缩放范围
                            scale = max(1.0, scale)
                        },
                    DragGesture()
                        .onChanged { value in
                            // 基于拖动开始时的偏移量计算当前偏移
                            offset = CGSize(
                                width: accumulatedOffset.width + value.translation.width,
                                height: accumulatedOffset.height + value.translation.height
                            )
                        }
                        .onEnded { value in
                            // 更新累计偏移量
                            accumulatedOffset = offset
                        }
                )
            )
            .animation(.easeInOut, value: scale) // 添加平滑动画
    }
}
